# Script to Do Vignette Analysis
# Script created 9/2/2021 by AJH
# Script last edited 11/12/2021 by AJH
#########################################

library(tidyverse)
library(janitor)
library(cowplot)
library(sf)
library(gridExtra)
library(stargazer)
library(lfe)


# load in data 
load("cai_survey.Rdata") # Alyssa
dat <- dat %>% filter(last_answer >= 79)

#####################
# Cleaning/Data Prep
#####################

# Since these var's have silly names, here's what they are:
  # The C variables are the vignette set (1,2,3). For the first two vignettes, the questions are:
    # 1: "How difficult do you think it would be for the neighbors to organize in the first colonia?"
    # 2: "How difficult do you think it would be for the neighbors to organize in the second colonia?"
    # 3: "Which colonia(s) do you think the neighbors will organize?"
  # For the third vignette, the questions are:
    # 1: How difficult do you think it would be for the neighbors to organize among themselves, without the government, in the first colonia?
    # 2: How difficult do you think it would be for the neighbors to organize among themselves, without the government, in the second colonia?
    # 3: How difficult do you think it would be for the neighbors to organize to ask the government for help in the first colonia?
    # 4: How difficult do you think it would be for the neighbors to organize to ask the government for help in the second colonia?



# The table shows up like this:
  # (for the first two vignettes)
  # Problem:                choiceX_prob1_1A        choiceX_prob2_2A
  # Neighbors' Response:    choiceX_neighbors_1A    choiceX_neighbors_2A
  # Result:                 choiceX_resp_1A         choiceX_resp_2A
  # and then we ask them about choiceX_prob2_1A (in both colonias- we decided not to vary the second problem)

# For the third vignette 
  # (for the first two vignettes)
  # Problem:                choiceX_prob1_1        choiceX_prob2_2
  # Neighbors' Response:    choiceX_neighbors_1    choiceX_neighbors_2
  # Result:                 choiceX_resp1_1         choiceX_resp_2
  #                         choiceX_resp2_1         choiceX_resp2_2
  # and then we ask them about choiceX_prob2_1 (in both colonias - we decided not to vary the second problem)


#############  
# Vignette 1 
#############
# select relevant variables for vignette 1
v1 <- dat %>% select(response_id, c1_1, c1_2, c1_3, 
                     choice1_prob1_1A, choice1_prob1_2A, 
                     choice1_neighbors_1A, choice1_neighbors_2A, 
                     choice1_resp2_1A, choice1_resp2_2A, 
                     choice1_prob2_1A, choice2_prob1_1A)
# separate out the two options 
  # first colonia
  v1_first <- v1 %>% select(response_id, c1_1, c1_3,
                            choice1_prob1_1A,
                            choice1_neighbors_1A,
                            choice1_resp2_1A,
                            choice1_prob2_1A) %>% 
                            rename(org_difficulty = c1_1,
                                   org_yesno = c1_3,
                                   prob1 = choice1_prob1_1A,
                                   neighbors = choice1_neighbors_1A,
                                   resp1= choice1_resp2_1A,
                                   prob2 =choice1_prob2_1A)
  
  # For each variable, create the variable names out of the question responses
  v1_first$org_difficulty <- factor(v1_first$org_difficulty, 
                                    levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                                               "Somewhat easy","Extremely easy"))
  v1_first$prob1 <- ifelse(v1_first$prob1 =="Problemas con la frecuencia y presion del suministro de agua", "water", "shootings")
  v1_first$neighbors <- case_when(
    v1_first$neighbors=="Compartieron informacion entre vecinos por WhatsApp" ~ "information",
    v1_first$neighbors=="No se organizaron" ~ "none",
    v1_first$neighbors== "Se organizaron entre vecinos para compartir su agua y pedir pipas privadas"|
    v1_first$neighbors== "Se organizaron entre vecinos para monitorear la colonia" ~"private",
    v1_first$neighbors=="Se organizaron para solicitar ayuda de la alcaldia" ~ "government",
    TRUE ~ NA_character_)
  v1_first$resp1 <- case_when(
    v1_first$resp1 == "La tasa de crimen se mantuvo"|
    v1_first$resp1 == "Sus problemas de agua seguían" ~ "Failure",
    v1_first$resp1 == "Se redujo la tasa de crimen"|
    v1_first$resp1 == "Se resolvieron sus problemas de agua" ~ "Success",
    TRUE~ NA_character_
  )
  v1_first$prob_same <- ifelse((v1_first$prob1=="water" & v1_first$prob2=="problemas con la frecuencia y presion del suministro de agua")|
                               (v1_first$prob1=="shootings" & v1_first$prob2=="un aumento en tiroteos por la colonia"),1,0)
  v1_first$org_yesno <-ifelse(v1_first$org_yesno=="Both Colonia Alcatraz and Colonia Magnolia"|
                              v1_first$org_yesno=="Colonia Alcatraz",1,0)
  v1_first$colonia <- 1
  
  # second colonia
  v1_second <- dat %>% select(response_id, c1_2, c1_3,
                        choice1_prob1_2A, 
                        choice1_neighbors_2A, 
                        choice1_resp2_2A, 
                        choice1_prob2_1A)%>% 
    rename(org_difficulty = c1_2,
           org_yesno = c1_3,
           prob1 = choice1_prob1_2A,
           neighbors = choice1_neighbors_2A,
           resp1= choice1_resp2_2A,
           prob2 =choice1_prob2_1A)
  
  # separate out the two options 
  v1_second <- v1 %>% select(response_id, c1_1, c1_3,
                            choice1_prob1_1A,
                            choice1_neighbors_1A,
                            choice1_resp2_1A,
                            choice1_prob2_1A) %>% 
    rename(org_difficulty = c1_1,
           org_yesno = c1_3,
           prob1 = choice1_prob1_1A,
           neighbors = choice1_neighbors_1A,
           resp1= choice1_resp2_1A,
           prob2 =choice1_prob2_1A)
  
  # For each variable, create the variable names out of the question responses
  v1_second$org_difficulty <- factor(v1_second$org_difficulty, 
                                    levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                                               "Somewhat easy","Extremely easy"))
  v1_second$prob1 <- ifelse(v1_second$prob1 =="Problemas con la frecuencia y presion del suministro de agua", "water", "shootings")
  v1_second$neighbors <- case_when(
    v1_second$neighbors=="Compartieron informacion entre vecinos por WhatsApp" ~ "information",
    v1_second$neighbors=="No se organizaron" ~ "none",
    v1_second$neighbors== "Se organizaron entre vecinos para compartir su agua y pedir pipas privadas"|
      v1_second$neighbors== "Se organizaron entre vecinos para monitorear la colonia" ~"private",
    v1_second$neighbors=="Se organizaron para solicitar ayuda de la alcaldia" ~ "government",
    TRUE ~ NA_character_)
  v1_second$resp1 <- case_when(
    v1_second$resp1 == "La tasa de crimen se mantuvo"|
      v1_second$resp1 == "Sus problemas de agua seguían" ~ "Failure",
    v1_second$resp1 == "Se redujo la tasa de crimen"|
      v1_second$resp1 == "Se resolvieron sus problemas de agua" ~ "Success",
    TRUE~ NA_character_
  )
  v1_second$prob_same <- ifelse((v1_second$prob1=="water" & v1_second$prob2=="problemas con la frecuencia y presion del suministro de agua")|
                                 (v1_second$prob1=="shootings" & v1_second$prob2=="un aumento en tiroteos por la colonia"),1,0)
  v1_second$org_yesno <-ifelse(v1_second$org_yesno=="Both Colonia Alcatraz and Colonia Magnolia"|
                                v1_second$org_yesno=="Colonia Magnolia",1,0)
  v1_second$colonia <- 2
  
  # make a "long" dataframe with each colonia as its own row
  v1 <- rbind(v1_first, v1_second)
  v1$vignette <- 1



# second vignette pair
v2 <- dat %>% select(response_id, c2_1, c2_2, c2_3, 
                     choice2_prob1_1A, choice2_prob1_2A, 
                     choice2_neighbors_1A, choice2_neighbors_2A, 
                     choice2_resp2_1A, choice2_resp2_2A, 
                     choice2_prob2_1A, choice2_prob1_1A)

# separate out the two options 
# first colonia
v2_first <- v2 %>% select(response_id, c2_1, c2_3,
                          choice2_prob1_1A,
                          choice2_neighbors_1A,
                          choice2_resp2_1A,
                          choice2_prob2_1A) %>% 
  rename(org_difficulty = c2_1,
         org_yesno = c2_3,
         prob1 = choice2_prob1_1A,
         neighbors = choice2_neighbors_1A,
         resp1= choice2_resp2_1A,
         prob2 =choice2_prob2_1A)

  # For each variable, create the variable names out of the question responses
  v2_first$org_difficulty <- factor(v2_first$org_difficulty, 
                                    levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                                               "Somewhat easy","Extremely easy"))
  v2_first$prob1 <- ifelse(v2_first$prob1 =="Problemas con la frecuencia y presion del suministro de agua", "water", "shootings")
  v2_first$neighbors <- case_when(
    v2_first$neighbors=="Compartieron informacion entre vecinos por WhatsApp" ~ "information",
    v2_first$neighbors=="No se organizaron" ~ "none",
    v2_first$neighbors== "Se organizaron entre vecinos para compartir su agua y pedir pipas privadas"|
      v2_first$neighbors== "Se organizaron entre vecinos para monitorear la colonia" ~"private",
    v2_first$neighbors=="Se organizaron para solicitar ayuda de la alcaldia" ~ "government",
    TRUE ~ NA_character_)
  v2_first$resp1 <- case_when(
    v2_first$resp1 == "La tasa de crimen se mantuvo"|
      v2_first$resp1 == "Sus problemas de agua seguían" ~ "Failure",
    v2_first$resp1 == "Se redujo la tasa de crimen"|
      v2_first$resp1 == "Se resolvieron sus problemas de agua" ~ "Success",
    TRUE~ NA_character_
  )
  v2_first$prob_same <- ifelse((v2_first$prob1=="water" & v2_first$prob2=="problemas con la frecuencia y presion del suministro de agua")|
                                 (v2_first$prob1=="shootings" & v2_first$prob2=="un aumento en tiroteos por la colonia"),1,0)
  v2_first$org_yesno <-ifelse(v2_first$org_yesno==" Both Colonia Dalia and Colonia Orquídea"|
                                v2_first$org_yesno=="Colonia Dalia",1,0)
  v2_first$colonia <- 1
  
  # second colonia
  v2_second <- dat %>% select(response_id, c1_2, c1_3,
                              choice2_prob1_2A, 
                              choice2_neighbors_2A, 
                              choice2_resp2_2A, 
                              choice2_prob2_1A)%>% 
    rename(org_difficulty = c1_2,
           org_yesno = c1_3,
           prob1 = choice2_prob1_2A,
           neighbors = choice2_neighbors_2A,
           resp1= choice2_resp2_2A,
           prob2 =choice2_prob2_1A)

# separate out the two options 
  v2_second <- v2 %>% select(response_id, c2_1, c2_3,
                             choice2_prob1_1A,
                             choice2_neighbors_1A,
                             choice2_resp2_1A,
                             choice2_prob2_1A) %>% 
    rename(org_difficulty = c2_1,
           org_yesno = c2_3,
           prob1 = choice2_prob1_1A,
           neighbors = choice2_neighbors_1A,
           resp1= choice2_resp2_1A,
           prob2 =choice2_prob2_1A)
  
  # For each variable, create the variable names out of the question responses
  v2_second$org_difficulty <- factor(v2_second$org_difficulty, 
                                     levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                                                "Somewhat easy","Extremely easy"))
  v2_second$prob1 <- ifelse(v2_second$prob1 =="Problemas con la frecuencia y presion del suministro de agua", "water", "shootings")
  v2_second$neighbors <- case_when(
    v2_second$neighbors=="Compartieron informacion entre vecinos por WhatsApp" ~ "information",
    v2_second$neighbors=="No se organizaron" ~ "none",
    v2_second$neighbors== "Se organizaron entre vecinos para compartir su agua y pedir pipas privadas"|
      v2_second$neighbors== "Se organizaron entre vecinos para monitorear la colonia" ~"private",
    v2_second$neighbors=="Se organizaron para solicitar ayuda de la alcaldia" ~ "government",
    TRUE ~ NA_character_)
  v2_second$resp1 <- case_when(
    v2_second$resp1 == "La tasa de crimen se mantuvo"|
      v2_second$resp1 == "Sus problemas de agua seguían" ~ "Failure",
    v2_second$resp1 == "Se redujo la tasa de crimen"|
      v2_second$resp1 == "Se resolvieron sus problemas de agua" ~ "Success",
    TRUE~ NA_character_
  )
  v2_second$prob_same <- ifelse((v2_second$prob1=="water" & v2_second$prob2=="problemas con la frecuencia y presion del suministro de agua")|
                                  (v2_second$prob1=="shootings" & v2_second$prob2=="un aumento en tiroteos por la colonia"),1,0)
  v2_second$org_yesno <-ifelse(v2_second$org_yesno=="Both Colonia Dalia and Colonia Orquídea"|
                                 v2_second$org_yesno=="Colonia Orquídea",1,0)
  v2_second$colonia <- 2
  
  # make a "long" dataframe where each colonia is its own row
  v2 <- rbind(v2_first, v2_second)
  v2$vignette <- 2
  
  # combine vignette 1 and vignette 2
  vignettes_1_2 <- rbind(v1,v2)

#############  
# Vignette 3 
#############
  
v3 <-  dat %>% select(response_id, c3_1, c3_2, c3_3, c3_4, 
                         choice1_prob1_1, choice1_prob1_2, 
                         choice1_neighbors_1, choice1_neighbors_2, 
                         choice1_resp1_1, choice1_resp1_2, 
                         choice1_prob2_1, choice1_resp1_1, 
                         choice1_resp2_1, choice1_resp2_2) 
  # First colonia 
  v3_first <-  v3 %>% select(response_id, c3_1, c3_3, 
                        choice1_prob1_1, 
                        choice1_neighbors_1,
                        choice1_resp1_1, 
                        choice1_prob2_1,
                        choice1_resp2_1) %>% 
                rename( org_difficulty_private = c3_1,
                        org_difficulty_gov = c3_3, 
                       prob1 = choice1_prob1_1, 
                       neighbors = choice1_neighbors_1,
                       resp1 = choice1_resp1_1, 
                       prob2 = choice1_prob2_1,
                       resp2 = choice1_resp2_1)
  
  v3_first$org_difficulty_private <- factor(v3_first$org_difficulty_private, 
         levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                    "Somewhat easy","Extremely easy"))
  v3_first$org_difficulty_gov <- factor(v3_first$org_difficulty_gov, 
                                        levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                                                   "Somewhat easy","Extremely easy"))
  
  v3_first$prob1 <- ifelse(v3_first$prob1 =="Problemas con la frecuencia y presion del suministro de agua", "water", "shootings")
  v3_first$neighbors <- case_when(
    v3_first$neighbors=="Compartieron informacion entre vecinos por WhatsApp" ~ "information",
    v3_first$neighbors== "Se organizaron entre vecinos  para monitorear la colonia entre ellos"|
      v3_first$neighbors== "Se organizaron entre vecinos para  para monitorear la colonia entre ellos"|
        v3_first$neighbors== "Se organizaron entre vecinos para compartir su agua y pedir pipas privadas"  ~"private",
    v3_first$neighbors=="Se organizaron para solicitar ayuda de la alcaldia" ~ "government",
    TRUE ~ NA_character_)
  v3_first$resp1 <- ifelse(v3_first$resp1 == "Crearon un grupo de vecinos que se junta regularmente", "formal", "informal")

  v3_first$resp2 <- case_when(
    v3_first$resp2 == "A pesar de sus esfuerzos, la tasa de crimen se mantuvo"|
      v3_first$resp2 == "A pesar de sus esfuerzos, sus problemas de agua seguían" ~ "Failure",
    v3_first$resp2 == "Gracias a sus esfuerzos, se redujo la tasa de crimen"|
      v3_first$resp2 == "Gracias a sus esfuerzos, se resolvieron sus problemas de agua" ~ "Success",
    TRUE~ NA_character_
  )
  v3_first$prob_same <- ifelse((v3_first$prob1=="water" & v3_first$prob2=="problemas con la frecuencia y presion del suministro de agua")|
                                 (v3_first$prob1=="shootings" & v3_first$prob2=="un aumento en tiroteos por la colonia"),1,0)
  v3_first$colonia <- 1
  
  
  # Second colonia 
  v3_second <-  v3 %>% select(response_id, c3_2, c3_4, 
                             choice1_prob1_2, 
                             choice1_neighbors_2,
                             choice1_resp1_2, 
                             choice1_prob2_1,
                             choice1_resp2_2) %>% 
    rename( org_difficulty_private = c3_2,
            org_difficulty_gov = c3_4, 
            prob1 = choice1_prob1_2, 
            neighbors = choice1_neighbors_2,
            resp1 = choice1_resp1_2, 
            prob2 = choice1_prob2_1,
            resp2 = choice1_resp2_2)
  
  v3_second$org_difficulty_private <- factor(v3_second$org_difficulty_private, 
                                            levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                                                       "Somewhat easy","Extremely easy"))
  v3_second$org_difficulty_gov <- factor(v3_second$org_difficulty_gov, 
                                        levels = c("Extremely difficult", "Somewhat difficult", "Neither easy nor difficult", 
                                                   "Somewhat easy","Extremely easy"))
  
  v3_second$prob1 <- ifelse(v3_second$prob1 =="Problemas con la frecuencia y presion del suministro de agua", "water", "shootings")
  v3_second$neighbors <- case_when(
    v3_second$neighbors=="Compartieron informacion entre vecinos por WhatsApp" ~ "information",
    v3_second$neighbors== "Se organizaron entre vecinos  para monitorear la colonia entre ellos"|
      v3_second$neighbors== "Se organizaron entre vecinos para  para monitorear la colonia entre ellos" |
      v3_first$neighbors== "Se organizaron entre vecinos para compartir su agua y pedir pipas privadas"~"private",
    v3_second$neighbors=="Se organizaron para solicitar ayuda de la alcaldia" ~ "government",
    TRUE ~ NA_character_)
  v3_second$resp1 <- ifelse(v3_second$resp1 == "Crearon un grupo de vecinos que se junta regularmente", "formal", "informal")

  v3_second$resp2 <- case_when(
    v3_second$resp2 == "A pesar de sus esfuerzos, la tasa de crimen se mantuvo"|
      v3_second$resp2 == "A pesar de sus esfuerzos, sus problemas de agua seguían" ~ "Failure",
    v3_second$resp2 == "Gracias a sus esfuerzos, se redujo la tasa de crimen"|
      v3_second$resp2 == "Gracias a sus esfuerzos, se resolvieron sus problemas de agua" ~ "Success",
    TRUE~ NA_character_
  )
  v3_second$prob_same <- ifelse((v3_second$prob1=="water" & v3_second$prob2=="problemas con la frecuencia y presion del suministro de agua")|
                                 (v3_second$prob1=="shootings" & v3_second$prob2=="un aumento en tiroteos por la colonia"),1,0)
  v3_second$colonia <- 1
  
  v3 <- rbind(v3_first, v3_second)
  
  rm(list=setdiff(ls(), c("vignettes_1_2", "v3", "dat")))
  ###############################################################
  # Confirming that the algorithm worked and cleaning is correct
  ###############################################################
  # If everything looks good:
    # The v1_v2 database should have four observations per respondent
    # each of the variables should be balanced across treatments 
  responses_per_respondent <- vignettes_1_2 %>% group_by(response_id) %>% tally()
  prop.table(table(vignettes_1_2$prob1))
  prop.table(table(vignettes_1_2$neighbors))
  prop.table(table(vignettes_1_2$prob2))
  prop.table(table(vignettes_1_2$prob_same))

  responses_per_respondent <- v3 %>% group_by(response_id) %>% tally()
  prop.table(table(v3$prob1))
  prop.table(table(v3$neighbors))
  prop.table(table(v3$resp1))
  prop.table(table(v3$resp2))
  prop.table(table(v3$prob2))
  prop.table(table(v3$prob_same))
  
  ###########################
  # Description of responses
  ###########################
  # Note that because this isn't forced choice, we don't expect 50% colonia 1 and 50% colonia 2 like a normal conjoint
  # Note also that we got a lot of NA's on some of the conjoint questions- maybe worht looking into more
  
  ggplot(vignettes_1_2[!is.na(vignettes_1_2$org_difficulty),]) + 
    geom_bar(aes(x= org_difficulty))+
    #scale_x_discrete(labels = c("Extremely \n difficult", "Somewhat \n difficult", "Neither \n difficult \n nor easy", "Somewhat \n easy", "Extremely \n easy")) + 
    labs(x= "", y = "", title = "How difficult would it be to organize the hypothetical neighbors?")
  
  difficulty <- v3 %>% pivot_longer(cols = c("org_difficulty_private", "org_difficulty_gov"), 
                      names_to = "org_type", names_prefix= "org_difficulty_", values_to = "difficulty")
  difficulty$org_type <- factor(difficulty$org_type,
                                levels = c("gov", "private"),
                                labels= c("Government help", "Private Solution"))
  
  pdf(file = "3_Output/Plots/vignettes/difficulty.pdf", height =4, width = 6)
  ggplot(difficulty[!is.na(difficulty$difficulty),]) + geom_bar(aes(x= difficulty, fill = org_type, group = org_type), position = "dodge") + 
    scale_x_discrete(labels = c("Extremely \n difficult", "Somewhat \n difficult", "Neither \n difficult \n nor easy", "Somewhat \n easy", "Extremely \n easy")) + 
    labs(x= "", y = "",fill = "Type of solution to pursue", title = "How difficult would it be to organize the  hypothetical \n neighbors to pursue different solutions? \n Average Responses")+
    theme_classic()+    
    theme(legend.position="bottom")+
    scale_fill_manual(values=c("lightskyblue2", "royalblue4"))
  dev.off()

  
  rm(list=setdiff(ls(), c("vignettes_1_2", "v3", "dat")))
  
  ###########################
  # Analysis : Vignette 1/2
  ###########################
  # need to set baseline levels for each factor variable
  # I'm currently setting them as follows:
    #prob1: water
    # neighbors: did not organize
    # problem: different problem 
    # resp: failure 
  
  vignettes_1_2$org_difficulty <- as.numeric(factor(vignettes_1_2$org_difficulty,
                                                    levels = c("Extremely easy", "Somewhat easy", "Neither easy nor difficult", "Somewhat difficult", "Extremely difficult"), ordered=TRUE))
  vignettes_1_2$prob1 <- factor(vignettes_1_2$prob1, 
                               levels = c("water", "shootings"))
  vignettes_1_2$neighbors <- factor(vignettes_1_2$neighbors, 
                                levels = c("none", "government", "information", "private"))
  vignettes_1_2$resp1 <- factor(vignettes_1_2$resp1, 
                                    levels = c("Failure", "Success"))
  vignettes_1_2$prob_same <- factor(vignettes_1_2$prob_same, 
                                levels = c(0, 1))
  # simplest form
  m1 <- lm(org_difficulty ~ prob1 + neighbors + resp1 + prob_same, data=vignettes_1_2)
  se1 <- sqrt(diag(vcov(m1)))
  stargazer(m1, type = "text")

  coefs <- tibble(cat = c("Issue: Shootings (vs. water)", "Neighbors: Requested help from government", "Neighbors: Shared information", "Neighbors: Pursued a private solution",
                          "Initial Outcome: Efforts were successful", "New Problem: Same as old"),
                  intercept = c(m1$coefficients[2], m1$coefficients[3], m1$coefficients[4],  m1$coefficients[5], m1$coefficients[6], m1$coefficients[7]),
                  upper = c(m1$coefficients[2]+ 1.96*se1[2],m1$coefficients[3]+ 1.96*se1[3],m1$coefficients[4]+ 1.96*se1[4],m1$coefficients[5]+ 1.96*se1[5], m1$coefficients[6]+ 1.96*se1[6], m1$coefficients[7]+ 1.96*se1[7]),
                  lower = c(m1$coefficients[2]- 1.96*se1[2],m1$coefficients[3]- 1.96*se1[3],m1$coefficients[4]- 1.96*se1[4],m1$coefficients[5]- 1.96*se1[5], m1$coefficients[6]- 1.96*se1[6], m1$coefficients[7]- 1.96*se1[7]))
  coefs$cat <- factor(coefs$cat,
                      levels = c("Issue: Shootings (vs. water)", "Neighbors: Requested help from government", "Neighbors: Shared information", "Neighbors: Pursued a private solution",
                                 "Initial Outcome: Efforts were successful", "New Problem: Same as old"),
                      ordered=TRUE)
  
  pdf(file = "3_Output/Plots/vignettes/vignette_1_2.pdf", height =4, width = 6)
  ggplot(coefs) + 
    geom_rect(ymin=-Inf, ymax=Inf, xmin = 0, xmax=1.5, fill = 'pink')+ 
    geom_rect(ymin=-Inf, ymax=Inf, xmin = 1.5, xmax=4.5, fill = 'cornflowerblue')+ 
    geom_rect(ymin=-Inf, ymax=Inf, xmin = 4.5, xmax=5.5, fill = 'cornsilk1')+ 
    geom_rect(ymin=-Inf, ymax=Inf, xmin = 5.5, xmax=6.5, fill = 'darkseagreen2')+
    geom_point(aes(x= cat, y = intercept))+ 
    geom_errorbar(aes(x=cat,ymin=lower, ymax = upper), width = .1)+ 
    theme_classic() +
    coord_flip()+
    labs(x= "", 
         y ="Point Estimate", 
         title = "Effects on Difficulty of Organizing",
         caption = paste("Estimates of effect on evaluated difficulty for neighbors of organizing (1-5 scale). \n Results for ", nobs(m1), " observations."), sep = "") +
    geom_hline(yintercept = 0, color = "red", linetype = "dotted")  
  dev.off()
  
  
  # I also want to do a coefficient on "any organizing" versus no organizing
  vignettes_1_2$any_org <- ifelse(vignettes_1_2$neighbors != "none",1,0)
  m0 <- lm(org_difficulty ~ any_org, data=vignettes_1_2)
  se0 <- sqrt(diag(vcov(m1)))
  
  coefs_2 <- tibble(cat = c("Organized: any", "Requested help from government", "Shared information", "Pursued a private solution"),
                    intercept = c(m0$coefficients[2], m1$coefficients[3], m1$coefficients[4],  m1$coefficients[5]),
                    upper = c(m0$coefficients[2] + 1.96*se0[2],m1$coefficients[3]+ 1.96*se1[3],m1$coefficients[4]+ 1.96*se1[4],m1$coefficients[5]+ 1.96*se1[5]),
                    lower = c(m0$coefficients[2] - 1.96*se0[2],m1$coefficients[3]- 1.96*se1[3],m1$coefficients[4]- 1.96*se1[4],m1$coefficients[5]- 1.96*se1[5]))
  coefs_2$cat <- factor(coefs_2$cat, 
                        levels = c("Pursued a private solution", "Requested help from government", "Shared information", "Organized: any"),
                        labels = c("Pursued a  \n private solution", "Requested help \n from government", "Shared \n information", "Organized: any"))
  # This plot is just showing organizing outcomes 
  pdf(file = "3_Output/Plots/vignettes/vignette_1_2_minimalist.pdf", height =4, width = 6)
  ggplot(coefs_2) + 
    geom_rect(ymin=-Inf, ymax=Inf, xmin = 3.5, xmax=4.5, fill = 'gray90')+ 
    geom_point(aes(x= cat, y = intercept))+ 
    geom_errorbar(aes(x=cat,ymin=lower, ymax = upper), width = .1)+ 
    theme_classic() +
    coord_flip()+
    labs(x= "", 
         y ="Point Estimate", 
         title = "Survey Experiment: History of Organizing Reduces \n Perceived Costs of Future Organizing",
         caption = paste("Estimates of effect on evaluated difficulty for neighbors of organizing (1-5 scale). \n Results for ", nobs(m1), " observations."), sep = "") +
    geom_hline(yintercept = 0, color = "red", linetype = "dotted") +
    theme(axis.text.y = element_text(face = c('plain', 'plain', 'plain', 'bold')))
  dev.off()
  
  

  ###########################
  # Analysis : Vignette 3
  ###########################
  # need to set baseline levels for each factor variable
  # I'm currently setting them as follows:
  # prob1: water
  # neighbors: information
  # resp1: informal 
  # resp2: failure
  # prob_same: different
  
  v3$org_difficulty_private <- as.numeric(factor(v3$org_difficulty_private, 
                                          levels = c("Extremely easy", "Somewhat easy", "Neither easy nor difficult", "Somewhat difficult", "Extremely difficult"),
                                          ordered=TRUE))
  v3$org_difficulty_gov <- as.numeric(factor(v3$org_difficulty_gov, 
                                      levels = c("Extremely easy", "Somewhat easy", "Neither easy nor difficult", "Somewhat difficult", "Extremely difficult"),
                                      ordered=TRUE))
  
  v3$prob1 <- factor(v3$prob1, 
                                levels = c("water", "shootings"))
  v3$neighbors <- factor(v3$neighbors, 
                                    levels = c( "information", "government", "private"))
  v3$resp1 <- factor(v3$resp1, 
                     levels = c("informal", "formal"))
  v3$resp2 <- factor(v3$resp2, 
                                levels = c("Failure", "Success"))
  v3$prob_same <- factor(v3$prob_same, 
                                    levels = c(0, 1))
  
  # Requesting government help
  m4 <- lm(org_difficulty_gov ~ prob1 + neighbors + resp1 + resp2 + prob_same, data=v3)
  se4 <- sqrt(diag(vcov(m4)))
  stargazer(m4, type = "text")
  
  coefs_4 <- tibble(cat = c("Issue: Shootings (vs. water)", "Intermediate outcome: Formal institution created",  "Outcome: Efforts were successful", "New Problem: Same as old"),
                  intercept = c(m4$coefficients[2],  m4$coefficients[5], m4$coefficients[6], m4$coefficients[7]),
                  upper = c(m4$coefficients[2]+ 1.96*se4[2],m4$coefficients[5]+ 1.96*se4[5], m4$coefficients[6]+ 1.96*se4[6], m4$coefficients[7]+ 1.96*se4[7]),
                  lower = c(m4$coefficients[2]- 1.96*se4[2],m4$coefficients[5]- 1.96*se4[5], m4$coefficients[6]- 1.96*se4[6], m4$coefficients[7]- 1.96*se4[7]))
  coefs_4$cat <- factor(coefs_4$cat,
                      levels = c("Issue: Shootings (vs. water)",  "Intermediate outcome: Formal institution created",  "Outcome: Efforts were successful", "New Problem: Same as old"),
                      ordered=TRUE)
  
  # Organizing for private solution
  m5 <- lm(org_difficulty_private ~ prob1 + neighbors + resp1 + resp2 + prob_same, data=v3)
  se4 <- sqrt(diag(vcov(m5)))
  stargazer(m5, type = "text")
  
  coefs_5 <- tibble(cat = c("Issue: Shootings (vs. water)",   "Intermediate outcome: Formal institution created",  "Outcome: Efforts were successful", "New Problem: Same as old"),
                    intercept = c(m5$coefficients[2],  m5$coefficients[5], m5$coefficients[6], m5$coefficients[7]),
                    upper = c(m5$coefficients[2]+ 1.96*se4[2],m5$coefficients[5]+ 1.96*se4[5], m5$coefficients[6]+ 1.96*se4[6], m5$coefficients[7]+ 1.96*se4[7]),
                    lower = c(m5$coefficients[2]- 1.96*se4[2],m5$coefficients[5]- 1.96*se4[5], m5$coefficients[6]- 1.96*se4[6], m5$coefficients[7]- 1.96*se4[7]))
  coefs_5$cat <- factor(coefs_5$cat,
                        levels = c("Issue: Shootings (vs. water)",   "Intermediate outcome: Formal institution created",  "Outcome: Efforts were successful", "New Problem: Same as old"),
                        ordered=TRUE)

  
  coefs_4$outcome="To request government help"
  coefs_5$outcome="To pursue a private solution"
  coefs <- bind_rows(coefs_4, coefs_5)
  coefs$cat <- factor(coefs$cat,
                        levels = c("Issue: Shootings (vs. water)",  "New Problem: Same as old", "Intermediate outcome: Formal institution created",  "Outcome: Efforts were successful"),
                      labels = c("Original Problem \n (Shootings vs water)", "New Problem:\n Same as old", "Intermediate outcome: \n Formal institution \n created",  "Outcome:\n Efforts were \n successful"),
                      ordered=TRUE)
  
  # Combine private and government outcomes
  pdf(file = "3_Output/Plots/vignettes/vignette_3_new.pdf", height =6, width = 6)
  ggplot(coefs) +  
    geom_point(data=coefs, aes(x=cat, y = intercept, color = outcome), position = position_dodge(width=.9))+
    geom_errorbar(data = coefs, aes(x=cat,ymin=lower, ymax = upper, color = outcome), width = .1, position=position_dodge(width=.9))+ 
    theme_classic() +
    coord_flip()+
    labs(color="",
      x= "", 
         y ="Point Estimate", 
      title = "Survey Experiment: Effects of Randomized  \n Attributes of Past Organizing on Perceived \n Difficulty of Future Organizing",
      outcome= "Difficulty of organizing",
         caption = paste("Estimates of effect on perceived difficulty for neighbors to organize (1-5 scale). \n Results for ", nobs(m4), " observations.\n 95% Confidence Intervals"), sep = "") +
    geom_hline(yintercept = 0, color = "red", linetype = "dotted")  +
    theme(legend.position = "bottom")
  dev.off()

 